﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinFu.IoC.Configuration;
using LinFu.IoC.Factories;
using LinFu.IoC.Interfaces;
using LinFu.IoC;

namespace CoreEx.Wcf.Common.Implementation
{
    [Preprocessor]
    public class ServiceContractPreProcessor : IPreProcessor
    {        
        public void Preprocess(IServiceRequest request)
        {
            if (request.ServiceType != typeof(IServiceContractResolver))
            {
                if (request.ProposedFactory == null && request.ServiceType.IsServiceContract())
                {
                    var channelProxyFactory = request.Container.GetService<IChannelProxyFactory>();
                    request.ActualFactory =
                        new FunctorFactory(r => channelProxyFactory.CreateChannelProxy(r.ServiceType));
                }
            }
        }
    }
}
